<?php
    // +----------------------------------------------------------------------
    // | 海豚PHP框架 [ DolphinPHP ]
    // +----------------------------------------------------------------------
    // | 版权所有 2016~2017 河源市卓锐科技有限公司 [ http://www.zrthink.com ]
    // +----------------------------------------------------------------------
    // | 官方网站: http://dolphinphp.com
    // +----------------------------------------------------------------------
    // | 开源协议 ( http://www.apache.org/licenses/LICENSE-2.0 )
    // +----------------------------------------------------------------------

    namespace app\member\validate;

    use app\member\model\MemberModel;
    use think\Validate;

    /**
     * Auth验证器
     * Class Auth
     *
     * @package app\member\validate
     */
    class Auth extends Validate
    {

        //定义验证规则
        protected $rule = [
            'password|密码'           => 'require|length:6,20',
            're_password|确认密码'      => 'require|confirm:password',
            'phone|手机号'             => 'require|number|regex:^1\d{10}|unique:member',
            'validate_code|验证码'     => 'require|number|length:6,6',
            'referrer_phone|推荐人手机号' => 'number|regex:^1\d{10}|different:phone|checkReferrerPhone',
        ];

        //定义验证提示
        protected $message = [
            'phone.require' => '请输入手机号码',
            'phone.number'  => '手机号码必须为11位数字',
            'phone.regex'   => '您输入的手机号码长度不正确',
            'phone.unique'  => '您的手机已注册，不可重复注册！',

            'referrer_phone.different' => '不能自己推荐自己哟！',
            'referrer_phone.regex'     => '推荐人手机号不正确',
            'referrer_phone.unique'    => '推荐人不存在！',

            'password.require' => '密码不能为空',
            'password.length'  => '密码长度6-20位',

            're_password.require' => '确认密码不能为空',
            're_password.confirm' => '两次密码不一致',

            'validate_code.require' => '验证码不能为空',
            'validate_code.number'  => '验证码必须为6位数数字',
            'validate_code.length'  => '请输入6位数验证码',
        ];

        //定义验证场景
        protected $scene = [
            //登录
            'login'           => ['phone' => 'require|number|regex:^1\d{10}','password'],
            //获取验证码(注册)
            'push_phone_code' => ['phone' => 'require|number|regex:^1\d{10}'],
            //获取验证码(找回密码)
            'get_pwd_code'    => ['phone' => 'require|number|regex:^1\d{10}|checkPhone'],
            //用户注册
            'register'        => ['phone' => 'unique:member','password','re_password','validate_code','referrer_phone'],

            'forget_pwd'      => ['phone' => 'require|number|regex:^1\d{10}','password','re_password','validate_code'],
        ];

        protected function checkPhone($rule,$value,$data)
        {

            return MemberModel::checkSelfPhone($value,$data['member_id']);
        }

        protected function checkReferrerPhone($rule,$value,$data)
        {

            return MemberModel::where('phone',$data['referrer_phone'])->find() ? true : '此推荐人不存在！';
        }

    }
